package com.example.demo.project; /**
 * 编辑人：王其
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

// 存款记录类
class Deposit {
    private String accountNumber;
    private String ownerName;
    private String depositType;
    private double amount;
    private int term; // 存期（月）
    private Date depositDate;
    private Date maturityDate;
    private double interest;
    private InterestCalculator calculator;

    public Deposit(String accountNumber, String ownerName, String depositType, double amount, int term) {
        this.accountNumber = accountNumber;
        this.ownerName = ownerName;
        this.depositType = depositType;
        this.amount = amount;
        this.term = term;
        this.depositDate = new Date();
        if ("定期".equals(depositType)) {
            this.calculator = new FixedDepositCalculator();
        } else {
            this.calculator = new DemandDepositCalculator();
        }
        calculateMaturityDate();
        calculateInterest();
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getBalance() {
        return amount + interest;
    }

    public double getInterest() {
        return interest;
    }

    // 获取时间
    private void calculateMaturityDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(depositDate);
        calendar.add(Calendar.MONTH, term);
        this.maturityDate = calendar.getTime();
    }

    // 获取总金额
    private void calculateInterest() {
        interest = calculator.calculateInterest(amount, term);
    }

    // 展示取款后金额
    public void withdraw() {
        System.out.println("存款金额：" + getBalance());
    }

    // 展示用户信息
    public void displayInfo() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if ("定期".equals(depositType)) {
            System.out.println("存款人姓名：" + ownerName);
            System.out.println("账号：" + accountNumber);
            System.out.println("存款金额：" + amount);
            System.out.println("到期时间：" + dateFormat.format(maturityDate));
            System.out.println("到期利息：" + interest);
        } else {
            System.out.println("存款人姓名：" + ownerName);
            System.out.println("账号：" + accountNumber);
            System.out.println("存款金额：" + amount);
            System.out.println("余额：" + (amount + interest));
        }
    }
}
